﻿#region License
// Copyright (c) 2009 Shengmin Zhang
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

using Apprender.Web.Configuration;

namespace Apprender.Web
{
    /// <summary>
    /// Build html tags
    /// </summary>
    public class HtmlBuilder : TagBuilder
    {
        public HtmlBuilder() { }
        
        public string Script(string src)
        {
            var attributes = this.GetScriptAttributes(src);
            return base.Render(src, attributes, TagType.Normal,"");
        }

        public void Script(string src, StringBuilder sb)
        {
            var attributes = this.GetScriptAttributes(src);
            base.Populate("script", attributes, TagType.Normal, sb,"");            
        }

        internal void Script(IList<IClientResource> resx, StringBuilder sb)
        {
            var attributes = new Dictionary<string, string>(1)
            {                
                {"type","text/javascript"}
            };

            base.PopulateWithBeginTag(sb, "script");
            base.PopulateWithAttributes(sb, attributes);

            sb.Append(@"src=""res.axd?t=js");
            for (int i = 0; i < resx.Count; i++)
            {
                var res = resx[i];
                sb.Append("&p=");
                sb.Append(res.Path);
            }
            sb.Append(@"""");

            base.PopulateWithEndTag(sb, "script", TagType.Normal, "");
        }

        
        private Dictionary<string, string> GetScriptAttributes(string src)
        {
            var attributes = new Dictionary<string, string>(2)
            {
                {"src",src},
                {"type","text/javascript"}
            };            
            return attributes;
        }

        public void Link(string href, StringBuilder sb)
        {
            var attributes = this.GetLinkAttributes(href);
            base.Populate("link", attributes, TagType.SelfClosing, sb,"");
        }

        internal void Link(IList<IClientResource> resx, StringBuilder sb)
        {
            var attributes = new Dictionary<string, string>(2)
            {
                {"rel","stylesheet"},
                {"type","text/css"}                
            };

            base.PopulateWithBeginTag(sb, "link");
            base.PopulateWithAttributes(sb, attributes);

            sb.Append(@"href=""res.axd?t=css");
            for (int i = 0; i < resx.Count; i++)
            {
                var res = resx[i];
                sb.Append("&p=");
                sb.Append(res.Path);
            }
            sb.Append(@"""");

            base.PopulateWithEndTag(sb, "link", TagType.SelfClosing, "");
        }

        private Dictionary<string, string> GetLinkAttributes(string href)
        {
            var attributes = new Dictionary<string, string>(3)
            {
                {"rel","stylesheet"},
                {"type","text/css"},
                {"href",href}
            };

            return attributes;
        }
    }
}
